package com.iLoo.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iLoo.entity.User;
import com.iLoo.mapper.UserMapper;
import com.iLoo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        //1.根据用户输入用户名进行查询
        User userDB = userMapper.findByUserName(user.getUsername());
        if(!ObjectUtils.isEmpty((userDB))){
            //2.比较密码
            if(userDB.getPassword().equals(user.getPassword())){
                return userDB;
            }else{
                throw new RuntimeException("密码输入并不正确！");
            }
        }else{
            throw new RuntimeException("用户名输入错误！");
        }
    }

    @Override
    public boolean register(User user) {
        //0.根据用户输入用户名判断用户是否存在
        User userDB= userMapper.findByUserName(user.getUsername());
        if(userDB==null){
            //1.生成用户状态
            user.setStatus("已激活");
            //2.设置用户注册时间
            user.setRegisterTime(new Date());
            //3.调用DAO
            userMapper.save(user);
            return true;
        }else {
            return false;
        }



    }
}
